const { read, write } = require('../lib/read&write')

const PASSWORD = '../data/password.json'
function setPassword(password) {
  const __Password = read(PASSWORD)
  __Password.push(password)
  write(__Password, PASSWORD)
}

function editPassword(password) {
  const __Password = read(PASSWORD)
  __Password.splice(
    __Password.findIndex((item) => item.account == password.account),
    1,
    password,
  )
  write(__Password, PASSWORD)
}

function deletePassword(passwordId) {
  const __Password = read(PASSWORD)
  let targetPassword = searchPassword(passwordId)
  __Password.splice(
    __Password.findIndex((item) => item.account == targetPassword.id),
    1
  )
  write(__Password, PASSWORD)
}

function getAllPasswords() {
  const __Password = read(PASSWORD)
  return __Password
}

function searchPassword(passwordId) {
  const __Password = read(PASSWORD)
  return __Password.find((item) => item.account === passwordId)
}

module.exports = {
  searchPassword,
  getAllPasswords,
  editPassword,
  setPassword,
  // 以下未用
  deletePassword,
}
